package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.util.ResultProductList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class SearchAutoCompleteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String keyword = req.getParameter("keyword");
        CatalogService catalogService = new CatalogService();

        List<Product> rsList = catalogService.searchProductList(keyword);

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();

        ResultProductList resultProductList = new ResultProductList();
        resultProductList.setProductList(rsList);

        String rt = JSON.toJSONString(resultProductList);
        out.print(rt);

        out.flush();
        out.close();
    }
}
